package com.wuxf.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuxf.entity.PwdInfo;
import com.wuxf.entity.TicketInfo;
import com.wuxf.entity.custom.PwdCustom;
import com.wuxf.mapper.custom.PwdMapper;
import com.wuxf.util.CommonUtils;
import com.wuxf.util.DateUtils;

@Service
@SuppressWarnings({"rawtypes","unchecked"})
public class PwdService {
	@Autowired
	private TicketService ticketService;
	@Autowired
	private PwdMapper pwdMapper;
	
	
	public PageInfo<List<PwdCustom>> findByMapAndPage(PageInfo pageInfo,HashMap paraMap) {
		PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
		return new PageInfo(pwdMapper.findByMap(paraMap));
	}
	
	public List<PwdCustom> findByMap(HashMap paraMap) {
		return pwdMapper.findByMap(paraMap);
	}
	
	
	public Map generatePwd(int userId,String ticketType,int receiveId){
		Map resultMap = new HashMap();
		TicketInfo ticketInfo = ticketService.findResidueTicketNum(userId, ticketType);
		if(ticketInfo.getTicketNum()>0) {
			ticketInfo.setTicketNum(ticketInfo.getTicketNum()-1);
			ticketService.updateTicket(ticketInfo);
			
			String pwdValue = ticketType+CommonUtils.getRandomString(4);
			PwdInfo pwdInfo = new PwdInfo();
			pwdInfo.setCreateTime(DateUtils.getNowDate());
			pwdInfo.setCreateUser(userId);
			pwdInfo.setReceiveId(receiveId);
			pwdInfo.setState("1");
			pwdInfo.setPwdValue(pwdValue);
			pwdInfo.setTicketType(ticketType);
			pwdMapper.savePwdInfo(pwdInfo);
			resultMap.put("pwdValue", pwdValue);
			resultMap.put("generateState", "1");
		}else {
			resultMap.put("msg", "您暂无生成密码权限,请及时购买!");
			resultMap.put("generateState", "2");
		}
		return resultMap;
	}
	
	public void updatePwdInfo(PwdInfo pwdInfo) {
		pwdInfo.setUpdateTime(DateUtils.getNowDate());
		pwdMapper.updatePwdInfo(pwdInfo);
	}
	
}
